home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / WWW / gform.1.1 / send_email.c < prev    next >
C/C++ Source or Header  |  1996-02-10  |  1KB  |  71 lines

  1. #include <string.h>
  2.  
  3. /***
  4.  * Send an email message
  5.  *
  6.  *  to - The intended recipient.
  7.  * sub - The subject of the message.
  8.  * bod - The message body.
  9.  *
  10.  * returns 0 on success
  11.  *      -1 on failure.
  12.  *
  13.  ***/
  14.  
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <sys/wait.h>
  18. #include <errno.h>
  19.  
  20. #include "config.h"
  21.  
  22. #ifndef MAILER
  23. #define MAILER "/usr/bin/mail"
  24. #endif
  25.  
  26.  
  27. void *malloc();
  28.  
  29. int send_email(to,sub,body)
  30. char *to, *sub, *body;
  31. {
  32.     int fd[2], pid;
  33.     FILE *f1;
  34.     char *msg;
  35.  
  36.     msg = (char *) malloc(strlen(body)+strlen(sub)+strlen(to)+16);
  37.  
  38.     sprintf(msg,"To: %s\n",to);
  39.     strcat(msg,"Subject: "); strcat(msg,sub);strcat(msg,"\n");
  40.     strcat(msg,body);
  41.  
  42. /* set child process STDIN to parent fd */
  43.     
  44.     if (pipe(fd) <0)
  45.     return(-1);    
  46.   
  47.     if ((pid = fork()) < 0)
  48.      return(-1);    
  49.     else if (pid >0) {            /* parent */
  50.     close(fd[0]);             /* close read end */
  51.     write(fd[1], msg, strlen(msg));
  52.         close(fd[1]);
  53.     if (waitpid(pid, NULL, 0) <0)
  54.         return(-1);    
  55. /*     close(fd[1]); */
  56.     return(0);
  57.     } else {        /* child */
  58.     close(fd[1]);    
  59.     if (fd[0] != STDIN_FILENO) {
  60.        if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO) 
  61.         return(-1);    
  62.        close(fd[0]);
  63.     }    /* invoke mailer which expects STDIN */
  64.         if (execl(MAILER,MAILER,to,0) <0) 
  65.               return(-1); 
  66.     close(fd[0]);
  67.     exit(0);
  68.     }
  69.     return(0);
  70. }
  71.